using System;
using System.Collections.Generic;
using System.Text;

namespace NBear.IoC.Service.Services
{
    [Serializable]
    public class MessageRequestCallbackHandler : MarshalByRefObject
    {
        private IServiceMQ mq;
        private IService service;

        public MessageRequestCallbackHandler(IServiceMQ mq, IService service)
        {
            this.mq = mq;
            this.service = service;
        }

        public IService Service
        {
            get
            {
                return service;
            }
        }

        #region IMessageRequestCallback Members

        public void OnMessageRequestCallback(RequestMessage header)
        {
            RequestMessage msg = mq.ReceiveRequestFromQueue(header.TransactionId);
            if (msg != null)
            {
                mq.SendResponseToQueue(service.CallService(msg));
            }
        }
        #endregion

        #region MarshalByRefObject

        public override object InitializeLifetimeService()
        {
            return null;
        }

        #endregion
    }
}
